package com.shiro.Realm;

import com.shiro.Dao.UserDao;
import com.shiro.VO.User;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.*;

/**
 * @author fangtongle
 * @version 1.0
 * @description 自定义Realm
 * @date 2018/4/20
 */
public class CustomRealm extends AuthorizingRealm {

    @Autowired
    private UserDao userDao;

    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        String userName = (String) principalCollection.getPrimaryPrincipal();
        // 模拟从数据库或者缓存(User -> Role)
        Set<String> roles = getRoleByUserName(userName);

        // 模拟从数据库或者缓存(User -> Permission)
        Set<String> permissions = getPermissionByUserName(userName);

        SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo();
        simpleAuthorizationInfo.setStringPermissions(permissions);
        simpleAuthorizationInfo.setRoles(roles);

        return simpleAuthorizationInfo;
    }


    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {

        // 1、从主体传过来的认证信息中获得用户名
        String userName = (String) authenticationToken.getPrincipal();

        // 2、通过用户名到数据库中获取凭证(password)
        String password = getPasswordByUserName(userName);
        if (password == null) {
            return null;
        }
        SimpleAuthenticationInfo authenticationInfo = new SimpleAuthenticationInfo(userName, password, "CustomRealm");

        // MD5加密
        authenticationInfo.setCredentialsSalt(ByteSource.Util.bytes(userName));

        return authenticationInfo;
    }

    /**
     * 模拟数据库
     *
     * @param userName
     * @return
     */
    private String getPasswordByUserName(String userName) {
        User user = userDao.getPasswordByUserName(userName);
        if (user != null) {
            return user.getPassword();
        }
        return null;
    }


    /**
     * UserName -> Roles
     *
     * @param userName
     * @return
     */
    private Set<String> getRoleByUserName(String userName) {
        System.out.println("------------> 从DB获取授权数据 <----------");
        List<String> list = userDao.getRoleByUserName(userName);
        Set<String> set = new HashSet<>(list);
        return set;
    }


    /**
     * UserName -> Permission
     *
     * @param userName
     * @return
     */
    private Set<String> getPermissionByUserName(String userName) {
        Set<String> sets = new HashSet<>();
        sets.add("user:delete");
        sets.add("user:update");
        return sets;
    }

}
